﻿/* 
 * 目的：用户登录并保存用户信息
 * 编写人：KH
 * 时间：2017/5/3
 * 修改人：
 * 修改时间：
 */
using FPC.Offer.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using NLog;

namespace FPC.Offer.UI.GeneralProcessor.Login
{
    /// <summary>
    /// UserLogin 的摘要说明
    /// </summary>
    public class UserLogin : IHttpHandler, IReadOnlySessionState
    {
        FPC.Offer.Service.Police_FuzzTableService PoliceMessage = new Service.Police_FuzzTableService();
        FPC.Offer.Service.Police_OrganizeTalbeService OrganizeMessige = new Service.Police_OrganizeTalbeService();
        private static Logger logger = LogManager.GetCurrentClassLogger();
        Base.BasePage lii = new Base.BasePage();
        /// <summary>
        /// 登录并保存用户信息，Session保存的是一个list集合，使用时new list方可.里面的字段。
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            //参数获取
            var Account = context.Request["Account"];
            var Pwd = context.Request["Pwd"];

            var ValidateCode = context.Request["ValidateCode"];
            var Acction = context.Request["Acction"];
            //登录
            if (Acction == EnumAction.Login.ToString())
            {
                //判断验证码输入是否正确
                if (ValidateCode.ToUpper().ToString() != context.Session[SystemKeys.SESSION_CODE].ToString())
                {
                    context.Response.Write(0);
                }
                else
                {
                    //通过账号获取对象
                    Model.Police_FuzzTable user = PoliceMessage.FindSingle(w => w.LoginName == Account);
                    if (user == null)
                    {
                        context.Response.Write(1);
                    }
                    else
                    {
                        //加密
                        var NewPwd = Md5.Encrypt(Pwd);
                        if (user.LoginPwd != NewPwd)
                        {
                            context.Response.Write(2);
                        }
                        else
                        {
                            //保存用户所有信息
                            context.Session[SystemKeys.SESSION_USERINFO] = user;
                            //保存用户昵称
                            context.Session[SystemKeys.COOKIE_USER_NAME_REMEMBER] = user.UserName;
                            //保存用户ID
                            context.Session[SystemKeys.COOKIE_USER_UserID_REMEMBER] = user.AlarmId;
                            //保存用户角色ID
                            context.Session[SystemKeys.COOKIE_USER_ROLE_REMEMBER] = user.RoleId;
                            //根据组织ID查询员工所在组织
                            var Organize = OrganizeMessige.FindSingle(p => p.OrganizeId == user.OrganizeId);                         
                            //保存组织名称
                            context.Session[SystemKeys.COOKIE_USER_OrganizeName_REMEMBER] = Organize.OrganizeName;
                            string cc = "";
                            cc = "操作人警号:" + user.AlarmId.ToString();
                            cc += ",操作人：" + user.UserName + "登录";                          
                            logger.Info(cc);
                            context.Response.Write(3);                 
                            FPC.Offer.Common.SysAccountLogin.Signin(user);
                        }
                    }
                }
            }
            //注销
            if (Acction == EnumAction.Cancel.ToString())
            {
                //清空Cookie安全退出
               
                FormsAuthentication.SignOut();
                HttpContext.Current.Session.Abandon();
                HttpContext.Current.Session[SystemKeys.COOKIE_USER_UserID_REMEMBER] = null;
                context.Response.Write("1");
            }
        }
        public string Role_Roght_Show(int id)
        {
            FPC.Offer.Service.System_RoleRightService RR = new Service.System_RoleRightService();
            int RoleId = id;          
            var GetRoleRight = RR.Find(t => t.RoleId == RoleId).ToList();
            Service.System_PowerTableService Power = new Service.System_PowerTableService();
            var GetPower = Power.Find(null);
            var GetString = "";
            foreach (var item in GetPower)
            {
                foreach (var items in GetRoleRight)
                {
                    if (item.PowerId == items.PowerId)
                    {
                        if (item.PowerSublevelId == 0)
                        {
                            GetString += "<dl id=\"menu-article\">";//<i class=\"Hui-iconfont\">&#xe616;</i>
                            GetString += "<dt>" + item.PowerName + "<i class=\"Hui-iconfont menu_dropdown-arrow\">&#xe6d5;</i></dt>";
                            GetString += "<dd>";
                            GetString += "   <ul>";
                            foreach (var itemss in GetPower)
                            {
                                if (items.PowerId == itemss.PowerSublevelId)
                                {
                                    GetString += "       <li><a data-href=\"" + itemss.PowerAddress + "\" data-title=\"" + itemss.PowerRemarks + "\" href=\"javascript:;\">" + itemss.PowerName + "</a></li>";
                                }
                            }
                            GetString += "     </ul>";
                            GetString += "  </dd>";
                            GetString += "   </dl>";
                        }
                    }
                }
            }
            return GetString;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}